package com.bookocean.infrastructure.persistence.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.bookocean.infrastructure.persistence.po.NovelPO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 小说 Mapper 接口
 * 
 * @author BookOcean
 * @since 2024-01-01
 */
@Mapper
public interface NovelMapper extends BaseMapper<NovelPO> {

    /**
     * 分页查询进行中的小说
     * 
     * @param offset 偏移量
     * @param pageSize 每页数量
     * @param isMultiplayer 是否多人创作
     * @return 小说列表
     */
    @Select("SELECT * FROM novel_po WHERE status IN (1, 2) AND is_multiplayer = #{isMultiplayer} " +
            "ORDER BY created_at DESC LIMIT #{offset}, #{pageSize}")
    List<NovelPO> selectOngoingNovels(@Param("offset") Integer offset, 
                                      @Param("pageSize") Integer pageSize,
                                      @Param("isMultiplayer") Boolean isMultiplayer);

    /**
     * 分页查询完结榜小说
     * 
     * @param offset 偏移量
     * @param pageSize 每页数量
     * @param sortType 排序类型（hottest: 最热, newest: 最新）
     * @return 小说列表
     */
    @Select("<script>" +
            "SELECT n.*, " +
            "COALESCE(like_count, 0) as total_likes, " +
            "COALESCE(comment_count, 0) as total_comments, " +
            "COALESCE(favorite_count, 0) as total_favorites " +
            "FROM novel_po n " +
            "LEFT JOIN (" +
            "  SELECT target_id, COUNT(*) as like_count FROM like_po WHERE target_type = 2 GROUP BY target_id" +
            ") l ON n.novel_id = l.target_id " +
            "LEFT JOIN (" +
            "  SELECT target_id, COUNT(*) as comment_count FROM comment_po WHERE target_type = 2 GROUP BY target_id" +
            ") c ON n.novel_id = c.target_id " +
            "LEFT JOIN (" +
            "  SELECT target_id, COUNT(*) as favorite_count FROM favorite_po WHERE target_type = 2 GROUP BY target_id" +
            ") f ON n.novel_id = f.target_id " +
            "WHERE n.status = 3 " +
            "<if test='sortType == \"hottest\"'>" +
            "ORDER BY (total_likes + total_comments + total_favorites) DESC, n.created_at DESC" +
            "</if>" +
            "<if test='sortType == \"newest\"'>" +
            "ORDER BY n.completed_at DESC" +
            "</if>" +
            "LIMIT #{offset}, #{pageSize}" +
            "</script>")
    List<NovelPO> selectCompletedNovels(@Param("offset") Integer offset, 
                                        @Param("pageSize") Integer pageSize,
                                        @Param("sortType") String sortType);

    /**
     * 查询用户参与的小说
     * 
     * @param userId 用户ID
     * @param offset 偏移量
     * @param pageSize 每页数量
     * @return 小说列表
     */
    @Select("SELECT n.* FROM novel_po n " +
            "INNER JOIN novel_participant_po p ON n.novel_id = p.novel_id " +
            "WHERE p.user_id = #{userId} AND p.status = 1 " +
            "ORDER BY n.updated_at DESC LIMIT #{offset}, #{pageSize}")
    List<NovelPO> selectUserParticipatingNovels(@Param("userId") Long userId,
                                                @Param("offset") Integer offset,
                                                @Param("pageSize") Integer pageSize);
}
